package com.xvheng.module.backendApi.service.impl;

import java.util.Date;
import java.util.List;
import com.xvheng.common.utils.DateUtils;
import com.xvheng.common.utils.uuid.IdUtils;
import com.xvheng.module.backendApi.domain.Coupon;
import com.xvheng.module.backendApi.domain.UserCoupon;
import com.xvheng.module.backendApi.mapper.CouponMapper;
import com.xvheng.module.backendApi.mapper.UserCouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xvheng.module.backendApi.mapper.MemberMapper;
import com.xvheng.module.backendApi.domain.Member;
import com.xvheng.module.backendApi.service.IMemberService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员个人信息Service业务层处理
 * 
 * @author xvheng
 * @date 2025-01-03
 */
@Service
public class MemberServiceImpl implements IMemberService 
{
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private UserCouponMapper userCouponMapper;
    @Autowired
    private CouponMapper couponMapper;

    /**
     * 查询会员个人信息
     * 
     * @param id 会员个人信息主键
     * @return 会员个人信息
     */
    @Override
    public Member selectMemberById(String id)
    {
        return memberMapper.selectMemberById2(id);
    }

    /**
     * 查询会员个人信息列表
     * 
     * @param member 会员个人信息
     * @return 会员个人信息
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增会员个人信息
     * 
     * @param member 会员个人信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMember(Member member)
    {
        //会员号生成机制是根据时间+手机号生成
        String mobile = member.getMobile();
        String lastFour = mobile.substring(mobile.length() - 4);
        long l = System.currentTimeMillis();
        lastFour = l+lastFour;
        member.setUserNo(lastFour);
        String s = IdUtils.fastSimpleUUID();
        member.setId(s);
        Date nowDate = DateUtils.getNowDate();
        member.setCreateTime(nowDate);
        member.setUpdateTime(nowDate);
        UserCoupon userCoupon = member.getUserCoupon();
        userCoupon.setId(IdUtils.fastSimpleUUID());
        userCoupon.setCreateTime(nowDate);
        userCoupon.setUserId(s);
        String couponId = userCoupon.getCouponId();
        Coupon coupon = couponMapper.selectCouponById(couponId);
        userCoupon.setName(coupon.getName());
        //存储卡信息课人员信息
        userCouponMapper.insertUserCoupon(userCoupon);
        //存储充值记录
        return memberMapper.insertMember(member);
    }

    /**
     * 修改会员个人信息
     * 
     * @param member 会员个人信息
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除会员个人信息
     * 
     * @param ids 需要删除的会员个人信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(String[] ids)
    {
        return memberMapper.deleteMemberByIds(ids);
    }

    /**
     * 删除会员个人信息信息
     * 
     * @param id 会员个人信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberById(String id)
    {
        return memberMapper.deleteMemberById(id);
    }
}
